package com.spark.check

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.google.gson.Gson
import com.spark.check.model.PendingTaskDetailModel
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.toast
import com.spark.common.ext.wrapIO
import com.spark.common.model.ResultModel
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.common.widget.Alert
import com.spark.common.widget.RejectAlert
import kotlinx.android.synthetic.main.frag_to_audit.*

/**
 * 待审核任务
 */
class ToAuditFragment : BaseFragment() {
    private var status = ""
    private var info = ""

    companion object {
        fun newInstance(): ToAuditFragment {
            return ToAuditFragment()
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_to_audit
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { activity?.finish() }
        everything.setOnClickListener { }

        fetchPendingTasks()
        tv_reject.setOnClickListener {
            activity?.apply {
                RejectAlert.Builder(this, "请说明驳回理由")
                        .affirm("确定") { d, i ->
                            d.dismiss()
                            submit(0, info)
                        }
                        .cancel("取消") { d, i ->
                            d.dismiss()
                        }
                        .create()
                        .show()
            }
        }
        tv_approve.setOnClickListener {
            activity?.apply {
                Alert.Builder(this, "是否审核通过")
                        .affirm("确定") { d, i ->
                            d.dismiss()
                            submit(status.toInt(), info)
                        }
                        .cancel("取消") { d, i ->
                            d.dismiss()
                        }
                        .create()
                        .show()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null) {
            when (requestCode) {
                2 -> when (resultCode) {
                    1 -> {
                        val type = data.getIntExtra("qr_type", -1)
                        val str = data.getStringExtra("info")
                        submit(type, str)
                    }
                }
            }
        }
    }

    /**
     * 获取待审阅任务
     */
    private fun fetchPendingTasks() {
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_task_detail}", mutableMapOf(Pair(Const.qr_taskid, "$id")).toJsonBody())
                    .wrapIO()
                    .subscribe(
                            {
                                Gson().fromJson(it, PendingTaskDetailModel::class.java)?.apply {
                                    when (code) {
                                        "200" -> {

                                        }
                                        "1001" -> {
                                            toast("人员信息有误")
                                        }
                                        else -> {
                                            toast(msg)
                                        }
                                    }
                                }
                            },
                            {
                                it.printStackTrace()
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                dispose = it
                                loading()
                            }
                    )
        }
    }

    /**
     * 提交审核意见
     * @param status 1 => 同意 0 => 驳回
     */
    private fun submit(status: Int, remark: String) {
        val params = mutableMapOf(
                Pair(Const.qr_taskid, "$id"),
                Pair(Const.qr_status, "$status"),
                Pair(Const.qr_remark, remark)
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_chk_leader_audit}", params.toJsonBody())
                    .wrapIO()
                    .subscribe(
                            {
                                Gson().fromJson(it, ResultModel::class.java)?.apply {
                                    when (code) {
                                        "200" -> {
                                            toast("审批提交成功")
                                            pop()
                                        }
                                        "1004" -> toast("审核失败")
                                    }
                                }
                            },
                            {
                                it.printStackTrace()
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                dispose = it
                                loading("提交中...")
                            }
                    )
        }

    }
}